package com.hs.hlw.credit.system.web.controller.api;

import com.hs.hlw.credit.system.business.dto.DataSourceDto;
import com.hs.hlw.credit.system.business.dto.ResponseDto;
import com.hs.hlw.credit.system.business.service.DataSourceService;
import com.hs.hlw.credit.system.common.util.BeanUtil;
import com.hs.hlw.credit.system.common.util.JsonUtil;
import com.hs.hlw.credit.system.data.primary.model.DataSource;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * DataSourceController
 * @author zhb
 * @date 2018/10/29
 */
@RestController
@RequestMapping("/api/data/source")
public class DataSourceController {
    private @Autowired DataSourceService dataSourceService;

    @GetMapping(path = "list")
    @ApiOperation(value = "获取数据源列表", notes = "获取数据源列表")
    public ResponseDto list(@RequestParam(required = false) @ApiParam(value = "状态(0: 无效, 1: 有效)") Short status) {
        return dataSourceService.list(status);
    }

    @PostMapping(path = "add")
    @ApiOperation(value = "新增数据源", notes = "新增数据源")
    public ResponseDto add(@RequestBody @Valid DataSourceDto.Add data) {
        return dataSourceService.add(BeanUtil.chang(data, DataSource.class));
    }

    @PostMapping(path = "update")
    @ApiOperation(value = "修改数据源", notes = "修改数据源")
    public ResponseDto add(@RequestBody @Valid DataSourceDto.Update data) {
        return dataSourceService.update(BeanUtil.chang(data, DataSource.class));
    }

    @PostMapping(path = "delete")
    @ApiOperation(value = "删除数据源", notes = "删除数据源")
    public ResponseDto delete(@RequestParam @ApiParam(value = "id列表(使用json数据，包含的属性有: id)", required = true) String list) {
        List<DataSource> _list = JsonUtil.fromJsonArray(list, DataSource.class);
        return dataSourceService.delete(_list);
    }

    @PostMapping(path = "updateStatus")
    @ApiOperation(value = "更新数据源状态", notes = "更新数据源状态")
    public ResponseDto updateStatus(@RequestParam @ApiParam(value = "id列表(使用json数据，包含的属性有: [id, status])", required = true) String list) {
        List<DataSource> _list = JsonUtil.fromJsonArray(list, DataSource.class);
        return dataSourceService.updateStatus(_list);
    }

    @GetMapping(path = "detail")
    @ApiOperation(value = "数据源详情", notes = "数据源详情")
    public ResponseDto detail(@RequestParam @ApiParam(value = "id", required = true) Integer id) {
        return dataSourceService.detail(id);
    }
}